All files / web/src/app/song/[code] page.tsx

0% Statements 0/324
0% Branches 0/1
0% Functions 0/1
0% Lines 0/324

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
import type { Metadata } from 'next'
import Link from 'next/link'
import { notFound } from 'next/navigation'
import { CelebrationBackground } from '@/components/song-share/CelebrationBackground'
import { PlayerHero } from '@/components/song-share/PlayerHero'
import { SharedSongPlayerCard } from '@/components/song-share/SharedSongPlayerCard'
import { TrophyTile } from '@/components/song-share/TrophyTile'
import { getSharedSong } from '@/lib/song-share/getSharedSong'
import { css } from '@styled/css'
import { vstack, hstack, wrap } from '@styled/patterns'

export const dynamic = 'force-dynamic'

interface Props {
  params: Promise<{ code: string }>
}

const ORIGIN = process.env.NEXT_PUBLIC_APP_URL ?? 'https://abaci.one'

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const { code } = await params
  const payload = await getSharedSong(code)

  if (!payload) {
    return { title: 'Song not found | Abaci.One', robots: { index: false, follow: false } }
  }

  const { player, song } = payload
  const title = `${player.emoji} A song for ${player.name}`
  const description = song.title
    ? `Listen to "${song.title}" — a celebration song made for ${player.name} on Abaci.One`
    : `A celebration song made for ${player.name} on Abaci.One`

  // Two surfaces, two purposes:
  //
  // - `embedUrl` → HTML iframe player. Twitter / X / Discord / Mastodon read
  //   `twitter:player` and load it inline, getting the full SyncedLyricsPlayer
  //   experience.
  //
  // - `previewVideoUrl` → direct MP4 (H.264 + AAC, ~1 MB). Apple's
  //   LinkPresentation (per TN3156) only autoplays a tap-to-listen overlay in
  //   iMessage when `og:video` (or `twitter:player:stream`) points at a
  //   downloadable media asset; an HTML page is explicitly NOT enough. The
  //   MP4 is the still OG artwork held as a video track behind the song's
  //   audio. See `/api/song-share/[code]/preview.mp4`.
  const embedUrl = `${ORIGIN}/embed/song/${code}`
  const previewVideoUrl = `${ORIGIN}/api/song-share/${code}/preview.mp4`

  return {
    title,
    description,
    // Keepsake links are private-by-link, not for search engines.
    robots: { index: false, follow: false },
    openGraph: {
      title,
      description,
      type: 'website',
      siteName: 'Abaci.One',
      videos: [
        {
          url: previewVideoUrl,
          secureUrl: previewVideoUrl,
          type: 'video/mp4',
          width: 1200,
          height: 630,
        },
      ],
    },
    twitter: {
      card: 'player',
      title,
      description,
      players: [
        {
          playerUrl: embedUrl, // Twitter/Discord/Mastodon → HTML iframe
          streamUrl: previewVideoUrl, // Apple/iMessage → direct MP4
          width: 480,
          height: 600,
        },
      ],
    },
  }
}

export default async function SharedSongPage({ params }: Props) {
  const { code } = await params
  const payload = await getSharedSong(code, { bumpView: true })
  if (!payload) notFound()

  const { player, song, stats, visibility } = payload
  const hasStats =
    stats.age != null ||
    stats.accuracyPct != null ||
    stats.bestCorrectStreak != null ||
    (stats.skills?.length ?? 0) > 0 ||
    (stats.highlights?.length ?? 0) > 0 ||
    !!stats.storyAngle

  return (
    <div
      data-component="shared-song-page"
      className={css({
        position: 'relative',
        minH: '100vh',
        py: { base: '32px', md: '48px' },
        px: '16px',
      })}
    >
      <CelebrationBackground />

      <main
        className={vstack({
          position: 'relative',
          alignItems: 'stretch',
          gap: '36px',
          maxW: '680px',
          mx: 'auto',
          zIndex: 1,
        })}
      >
        <PlayerHero emoji={player.emoji} name={player.name} songTitle={song.title} />

        {/* Integrated player + lyrics — the lyrics are the playback surface */}
        <SharedSongPlayerCard
          audioPath={song.audioPath}
          alignmentPath={song.alignmentPath}
          lyrics={song.sections}
          title={song.title}
          variant="full"
          autoPlay={visibility.autoPlay}
        />

        {/* Musical style */}
        {song.styles.length > 0 && (
          <div className={wrap({ gap: '8px', justifyContent: 'center' })}>
            {song.styles.map((s) => (
              <span
                key={s}
                className={css({
                  fontSize: '12px',
                  fontWeight: '600',
                  color: 'purple.700',
                  bg: 'rgba(192, 132, 252, 0.14)',
                  borderRadius: 'full',
                  px: '12px',
                  py: '5px',
                })}
              >
                {s}
              </span>
            ))}
          </div>
        )}

        {/* Opt-in stats — rebuilt as trophy tiles */}
        {hasStats && (
          <section
            data-section="stats"
            className={vstack({
              alignItems: 'stretch',
              gap: '16px',
              bg: 'white',
              borderRadius: '20px',
              border: '1px solid rgba(192, 132, 252, 0.2)',
              p: { base: '20px', md: '24px' },
              boxShadow: '0 8px 28px rgba(124, 58, 237, 0.08)',
            })}
          >
            <h2
              className={css({
                fontFamily: 'display',
                fontStyle: 'italic',
                fontWeight: '600',
                fontSize: '22px',
                color: 'purple.900',
                m: 0,
                display: 'flex',
                alignItems: 'center',
                gap: '8px',
              })}
            >
              <span aria-hidden="true">✨</span>
              <span>The story so far</span>
            </h2>

            {stats.storyAngle && (
              <p
                className={css({
                  fontFamily: 'display',
                  fontStyle: 'italic',
                  fontSize: '17px',
                  lineHeight: 1.5,
                  color: 'gray.700',
                  m: 0,
                })}
              >
                {stats.storyAngle}
              </p>
            )}

            {stats.highlights && stats.highlights.length > 0 && (
              <ul
                data-element="session-highlights"
                className={css({
                  display: 'flex',
                  flexDirection: 'column',
                  gap: '8px',
                  listStyle: 'none',
                  m: 0,
                  p: 0,
                })}
              >
                {stats.highlights.map((h) => (
                  <li
                    key={h}
                    className={css({
                      fontSize: '14px',
                      lineHeight: 1.5,
                      color: 'gray.800',
                      display: 'flex',
                      gap: '10px',
                      alignItems: 'flex-start',
                    })}
                  >
                    <span aria-hidden="true">✨</span>
                    <span>{h}</span>
                  </li>
                ))}
              </ul>
            )}

            <div className={wrap({ gap: '12px' })}>
              {stats.accuracyPct != null && (
                <TrophyTile
                  tone="emerald"
                  glyph="✓"
                  value={`${stats.accuracyPct}%`}
                  label="Accuracy"
                />
              )}
              {stats.problemsDone != null && stats.problemsTotal != null && (
                <TrophyTile
                  tone="sky"
                  glyph="∑"
                  value={`${stats.problemsDone}/${stats.problemsTotal}`}
                  label="Problems"
                />
              )}
              {stats.bestCorrectStreak != null && stats.bestCorrectStreak > 0 && (
                <TrophyTile
                  tone="amber"
                  glyph="🔥"
                  value={stats.bestCorrectStreak}
                  label="Best streak"
                />
              )}
              {stats.age != null && (
                <TrophyTile tone="rose" glyph="✦" value={stats.age} label="Age" />
              )}
            </div>

            {stats.skills && stats.skills.length > 0 && (
              <div className={hstack({ gap: '8px', flexWrap: 'wrap' })}>
                <span className={css({ fontSize: '12px', color: 'gray.500', fontWeight: '600' })}>
                  Skills practiced:
                </span>
                {stats.skills.map((sk) => (
                  <span
                    key={sk}
                    className={css({
                      fontSize: '12px',
                      fontWeight: '600',
                      color: 'emerald.800',
                      bg: 'emerald.50',
                      border: '1px solid token(colors.emerald.200)',
                      borderRadius: 'full',
                      px: '10px',
                      py: '3px',
                    })}
                  >
                    {sk}
                  </span>
                ))}
              </div>
            )}
          </section>
        )}

        {/* CTA pill — friendly, branded, doesn't compete with the player */}
        <div className={vstack({ alignItems: 'center', gap: '6px', pt: '4px', pb: '16px' })}>
          <Link
            href="/"
            className={css({
              display: 'inline-flex',
              alignItems: 'center',
              gap: '8px',
              px: '20px',
              py: '12px',
              bg: 'white',
              border: '1px solid token(colors.purple.200)',
              borderRadius: 'full',
              fontSize: '14px',
              fontWeight: '700',
              color: 'purple.800',
              textDecoration: 'none',
              boxShadow: '0 4px 14px rgba(251, 191, 36, 0.18)',
              transition: 'transform 0.15s, box-shadow 0.15s',
              _hover: {
                transform: 'translateY(-1px)',
                boxShadow: '0 6px 20px rgba(251, 191, 36, 0.28)',
              },
            })}
          >
            <span>Make a song on Abaci.One</span>
            <span aria-hidden="true">→</span>
          </Link>
          <p className={css({ fontSize: '12px', color: 'gray.500', m: 0 })}>
            Adaptive abacus practice that ends in a song.
          </p>
        </div>
      </main>
    </div>
  )
}